package cn.yunhe.controller;

import cn.yunhe.entity.Stock;
import cn.yunhe.service.IStockService;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 邓永凯
 */
@WebServlet("/after/StockQueryByKeyword")
public class StockQueryByKeyword extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取关键字
        String keyword = request.getParameter("keyword");
        //调用service的方法
        IStockService iStockService = new IStockService();
        List<Stock> stockList = iStockService.stockQuery(keyword);
        System.out.println(stockList.size());
        response.setContentType("text/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(stockList);
        out.print(jsonArray);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
